# encoding=utf8
import sys
import math
reload(sys)
sys.setdefaultencoding('utf8')
from ..obdMessageAnalysis import*

class OBD_02:
	#*****************************************************************************#
	#*Description：全局变量定义
	#*****************************************************************************#
	AnalysisSwitch=1    #解析开关，1：按17691法规解析      2：按17691法规纠正后的解析
	InfoPrintStruct=[]  #02信息数据结构
	result=[]           #输出结果
	FaultTol=0          #故障码总数

	#*****************************************************************************#
	#*Description：逻辑处理通道
	#*****************************************************************************#
	def LogicAisle(self, protocolStr, header):
		self.result = header
		self.InfoPrintStruct=[]
		name = ""
		len = 0
		offset = 0
		self.init()                                          #初始化
		for t in self.InfoPrintStruct:                       #轮询打印
			name = t['name']                                 #获取信号名称
			len = int(t['len'])                              #获取信号字段长度
			if len == -1:                                    #故障码长度获取
				if FaultTol >= 0xFE:
					len = 0
				else:
					len = FaultTol * 4;
			offset += t['fun'](name, protocolStr[offset:], len)   #字段解析,并更新偏移

	#*****************************************************************************#
	#*Description：02信息数据结构列表添加
	#*****************************************************************************#
	def AddList(self, name, len, fun):
		self.InfoPrintStruct.append({
			'name': name,
			'len':len,
			'fun': fun,
		})

	#*****************************************************************************#
	#*Description：初始化
	#*****************************************************************************#
	def init(self):
		self.AddList(name = "数据采集时间:",                 len = "6",  fun = self.FieldAnalysis)
		self.AddList(name = "信息流水号:",                   len = "2",  fun = self.FieldAnalysis)
		
		for i in range(10):
			self.AddList(name = "\n"+"======第"+str(i+1)+"包数据======",    len = "0",  fun = self.FieldAnalysis)
			self.AddList(name = "信息类型标志:",                 len = "1",  fun = self.FieldAnalysis)
			self.AddList(name = "OBD诊断协议:",                  len = "1",  fun = self.FieldAnalysis)
			self.AddList(name = "MIL状态:",                      len = "1",  fun = self.FieldAnalysis)
			self.AddList(name = "诊断支持状态:",                 len = "2",  fun = self.FieldAnalysis)
			self.AddList(name = "诊断就绪状态:",                 len = "2",  fun = self.FieldAnalysis)
			self.AddList(name = "车辆识别码:",                   len = "17", fun = self.FieldAnalysis)
			self.AddList(name = "软件标定识别号:",               len = "18", fun = self.FieldAnalysis)
			self.AddList(name = "标定验证码（CVN）:",            len = "18", fun = self.FieldAnalysis)
			self.AddList(name = "IUPR值:",                       len = "36", fun = self.FieldAnalysis)
			self.AddList(name = "故障码总数:",                   len = "1",  fun = self.FieldAnalysis)
			self.AddList(name = "故障码列表:",                   len = "-1", fun = self.FieldAnalysis)
			self.AddList(name = "信息类型标志:",                 len = "1",  fun = self.FieldAnalysis)
			self.AddList(name = "车速:",                         len = "2",  fun = self.FieldAnalysis)
			self.AddList(name = "大气压力（直接测量或估计值）:", len = "1",  fun = self.FieldAnalysis)
			self.AddList(name = "发动机净输出扭矩:",             len = "1",  fun = self.FieldAnalysis)
			self.AddList(name = "摩擦扭矩:",                     len = "1",  fun = self.FieldAnalysis)
			self.AddList(name = "发动机转速:",                   len = "2",  fun = self.FieldAnalysis)
			self.AddList(name = "发动机燃料流量:",               len = "2",  fun = self.FieldAnalysis)
			self.AddList(name = "SCR上游NOx传感器输出值:",       len = "2",  fun = self.FieldAnalysis)
			self.AddList(name = "SCR下游NOx传感器输出值:",       len = "2",  fun = self.FieldAnalysis)
			self.AddList(name = "反应剂余量:",                   len = "1",  fun = self.FieldAnalysis)
			self.AddList(name = "进气量:",                       len = "2",  fun = self.FieldAnalysis)
			self.AddList(name = "SCR入口温度:",                  len = "2",  fun = self.FieldAnalysis)
			self.AddList(name = "SCR出口温度:",                  len = "2",  fun = self.FieldAnalysis)
			self.AddList(name = "DPF压差:",                      len = "2",  fun = self.FieldAnalysis)
			self.AddList(name = "发动机冷却液温度:",             len = "1",  fun = self.FieldAnalysis)
			self.AddList(name = "油箱液位:",                     len = "1",  fun = self.FieldAnalysis)
			self.AddList(name = "定位状态:",                     len = "1",  fun = self.FieldAnalysis)
			self.AddList(name = "经度:",                         len = "4",  fun = self.FieldAnalysis)
			self.AddList(name = "纬度:",                         len = "4",  fun = self.FieldAnalysis)
			self.AddList(name = "累计里程:",                     len = "4",  fun = self.FieldAnalysis)

	#*****************************************************************************#
	#*Description：数据字段解析
	#*****************************************************************************#
	def FieldAnalysis(self, name, string, len):
		offset = len * 2
		global FaultTol                   #修改全局变量值，此处必须加global修饰

		if name == "数据采集时间:":
			self.result.append(name+"20"+str(int(string[0:2],16))+"年"+str(int(string[2:4],16))+"月"+str(int(string[4:6],16))+
			"日"+str(int(string[6:8],16))+"时"+str(int(string[8:10],16))+"分"+str(int(string[10:12],16))+"秒"+"\n")
		elif len ==0 and name == "故障码列表:":
			len = 0
		elif len == 0 and name != "故障码列表:":
			self.result.append(name + "\n")
		else:
			self.result.append(name + string[0:offset] + "\n")
			
		if name == "故障码总数:":         #记录故障码总数
			print("FaultTol = %s",string[0:offset])
			try:
		   		FaultTol = int(string[0:offset],16)
			except:
				print("int err! %s",string[0:offset])

		return offset
